package com.example.shop_car_2208.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.example.shop_car_2208.R
import com.example.shop_car_2208.adapter.GateAdapter
import com.example.shop_car_2208.adapter.GoodsAdapter
import com.example.shop_car_2208.adapter.OneAdapter
import com.example.shop_car_2208.databinding.FragmentCateBinding
import com.example.shop_car_2208.entity.CateEntity
import com.example.shop_car_2208.mvvm.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CateFragment : BaseFragment<FragmentCateBinding,CateViewModel>() {
    val catrLiset = mutableListOf<CateEntity>()
    //一级分类
    lateinit var oneAdapter: OneAdapter
    //二级分类
    lateinit var gateAdapter: GateAdapter
    //三级分类
    lateinit var goodsAdapter: GoodsAdapter
    var careId = 0
    var page = 1
    override fun initData() {
        viewModel.cateSuccess.observe(this){
            if (it.code == 200){
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //二级
                val data = oneAdapter.data[0].data
                if (data!=null){
                    gateAdapter.data.clear()
                    gateAdapter.data.addAll(data)
                    gateAdapter.notifyDataSetChanged()
                    careId = data[0].id
                    viewModel.getGood(careId,page,10)

                }
            }
        }
        viewModel.goodsSuccess.observe(this){
            if (it.code == 200){
                if (page == 1){
                    goodsAdapter.data.clear()
                }

                goodsAdapter.data.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)
        viewModel.getCate()
        gateAdapter = GateAdapter(catrLiset)
        binding.rvTwo.adapter = gateAdapter
        binding.rvTwo.layoutManager = LinearLayoutManager(context)
        //获取分类数据
        viewModel.getCate()
        goodsAdapter = GoodsAdapter()
        binding.rvGood.adapter = goodsAdapter
        binding.rvGood.layoutManager = GridLayoutManager(context,2)
        viewModel.getGood(careId,page,10)
        //一级分类点击事件
        oneAdapter.setOnItemClickListener { adapter, view, position ->

            //二级分类和三级分类清空
            gateAdapter.data.clear()
            goodsAdapter.data.clear()
            //获取二级分类的data
            val data = oneAdapter.data[position].data
            if (data!=null){
                if (data.isNotEmpty()){
                    oneAdapter.setSelectedId(oneAdapter.data[position].id)
                    gateAdapter.data.addAll(data)
                    //获取三级分类
                    careId = data[0].id
                    page = 1
                    viewModel.getGood(careId,page,10)
                }
            }
            gateAdapter.notifyDataSetChanged()
            goodsAdapter.notifyDataSetChanged()

        }
        gateAdapter.setOnItemClickListener { adapter, view, position ->
            gateAdapter.getSeleceId(gateAdapter.data[position].id)
            goodsAdapter.data.clear()
            val data = gateAdapter.data[position]
            if (data!=null){
                careId = data.id
                page = 1
                viewModel.getGood(careId,page,10)
            }
            goodsAdapter.notifyDataSetChanged()
            gateAdapter.notifyDataSetChanged()
        }
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/detail").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }
        binding.srl.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.getGood(careId,page,10)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.getGood(careId,page++,10)
                binding.srl.finishLoadMore()
            }

        })
    }

    override fun getViewModel(): Class<CateViewModel> {
        return CateViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.fragment_cate
    }

}